import Vue from 'vue'
import Router from 'vue-router'
import Login from './component/Login.vue'
import Home from './component/Home.vue'
import Welcome from './component/Welcome.vue'
import users from './component/user/Users.vue'
import Rights from './component/power/Rights.vue'
import roles from './component/power/Roles.vue'

Vue.use(Router)

const router = new Router({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
      path: '/home', component: Home,
      redirect: "/welcome",
      children: [
        { path: '/welcome', component: Welcome },
        { path: '/users', component: users },
        { path: '/Rights', component: Rights },
        { path: '/roles', component: roles }
      ]
    }
  ]
})


router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // nexe 是一个函数 代表放行
  // next() 放行 next('/login') 强制跳转
  if (to.path === "/login") {
    return next()
  } else {
    // 获取totke
    const tokenStr = window.sessionStorage.getItem('token')
    // 此处应该做一个验证 验证totke和服务器的是否一致
    if (!tokenStr) {
      next('/login')
    } else {
      next()
    }
  }
})


export default router
